/*
 * ComerciosBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 11:45 PM
 */
package org.contabilidad.juzgados;

import org.mybeans.Sesion;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author  avbravo
 */
public class JuzgadoEliminarFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    String idEntidad;

    /** Creates new form ComerciosBusquedaFrame */
    public JuzgadoEliminarFrame() {
        initComponents();
        sesion = new Sesion();


        /*
         * Fondo
         */

        
          
         

         
         

        /*
         * fin del fondo
         */
        AutoCompleteDecorator.decorate(this.jComboBoxJuzgado);
        CargarComboJuzgado();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jButtonEliminar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jComboBoxJuzgado = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ELIMINAR JUZGADOS");

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jLabel11.setText("Juzgado");

        jComboBoxJuzgado.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxJuzgadoItemStateChanged(evt);
            }
        });
        jComboBoxJuzgado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxJuzgadoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(183, 183, 183)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel11)
                        .addGap(18, 18, 18)
                        .addComponent(jComboBoxJuzgado, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jComboBoxJuzgado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonRegresarActionPerformed

    private void jComboBoxJuzgadoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxJuzgadoItemStateChanged
}//GEN-LAST:event_jComboBoxJuzgadoItemStateChanged

    private void jComboBoxJuzgadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxJuzgadoActionPerformed

        try {
            String temp = (String) jComboBoxJuzgado.getSelectedItem();
            if (temp == null || temp.equals("")) {
            } else {

                int posicion = temp.indexOf("|");

                int posfinal = 0;
                if (posicion <= 0) {
                    posfinal = 0;
                } else {
                    posfinal = posicion - 1;
                }

                String entidad = temp.substring(0, posfinal);
                int tamano = temp.length();
                int nuevaposicion = posfinal + 2;
                String idEntidad = temp.substring(nuevaposicion, tamano);

                this.idEntidad = idEntidad;

            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error  " + ex.getMessage(), "ComboBox SubGrupoRegimen ActionPerformed", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_jComboBoxJuzgadoActionPerformed

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        // TODO add your handling code here:
        try {
            if (this.idEntidad == null || this.idEntidad.equals("")) {
                JOptionPane.showMessageDialog(this, "Mensaje: no existe un codigo de entidad", "Advertencia", JOptionPane.WARNING_MESSAGE);
                return;

            }
            this.idEntidad = this.idEntidad.trim();
            int NumeroEntidad = Integer.parseInt(this.idEntidad);

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                sesion.con.close();
                return;
            }
            String sqlquery = "select * from vehiclessecuestroespera where idmunicipio=? and idjuzgado= ? and activo ='si'";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setInt(2, NumeroEntidad);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x != 0) {
                JOptionPane.showMessageDialog(this, "Existe un secuestro en espera asignado a este juzgado\n No se puede eliminar", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return;
            }

            int res = JOptionPane.showConfirmDialog(this, "Desea eliminar este juzgado",
                    "Eliminar Juzgado ", JOptionPane.YES_NO_OPTION);
            if (res == JOptionPane.YES_OPTION) {

                String sql = "delete from juzgado where idmunicipio=? and idjuzgado= ? and activo='si'";
                PreparedStatement prest = sesion.con.prepareStatement(sql);
                prest.setString(1, sesion.getIdmunicipio());
                prest.setInt(2, NumeroEntidad);

                prest.executeUpdate();
                sesion.con.close();
                JOptionPane.showMessageDialog(this, "Juzgado Eliminado exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                CargarComboJuzgado();
                this.idEntidad = "";
            } else {
                JOptionPane.showMessageDialog(this, "No se elimino el juzgado.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error  " + ex.getMessage(), "ComboBox SubGrupoRegimen ActionPerformed", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JuzgadoEliminarFrame().setVisible(true);
            }
        });
    }

    private void CargarComboJuzgado() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select juzgado.* from juzgado where idmunicipio='" + sesion.getIdmunicipio() + "' and activo='si' order by juzgado";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            ResultSet rs = psquery.executeQuery();
            jComboBoxJuzgado.removeAllItems();
            int count = 0;
            jComboBoxJuzgado.insertItemAt("", count);
            count = 1;
            while (rs.next()) {
                String entidad = rs.getString("juzgado");
                entidad = entidad + " | " + rs.getString("idjuzgado");
                jComboBoxJuzgado.insertItemAt(entidad, count);
                count++;
            }
            if (count != 0) {
                jComboBoxJuzgado.setSelectedIndex(0);
            }


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JComboBox jComboBoxJuzgado;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
